# -*- coding: utf-8 -*-
'''
Created on 11/ott/2009

@author: sax
'''
from xadmin.tests import ModelProviderTest, TestMasterModel, TestDetailModel, AdminModelManager
from xadmin import callbacks
from django.contrib import admin 
from xadmin.admin import XModelAdmin

class TestUtils(ModelProviderTest, AdminModelManager):
    def setUp(self):
        super(TestUtils, self).setUp()
        self._init_admin_model_manager()
    
    def test_get_email_href(self):
        column = callbacks.get_email_href('email_field')
        
        a = TestMasterModel()
        a.email_field =  "aa@bb.com"        
        self.assertEqual(column(None, a), "<a href='mailto:aa@bb.com'>aa@bb.com</a>")

    def test_get_email_href_for_empty_values(self):
        column = callbacks.get_email_href('email_field')
        
        a = TestMasterModel()
        a.email_field =  ""        
        self.assertEqual(column(None, a), "")

    def test_get_email_href_for_invalid_field(self):
        column = callbacks.get_email_href('www')
        
        a = TestMasterModel()
        a.email_field =  "a@b.com"
        self.assertEqual(column(None, a), "")

    def test_2(self):        
        self._register(TestMasterModel, XModelAdmin)
        self._register(TestDetailModel, XModelAdmin)
        
        a = TestMasterModel()
        a.email_field =  "aa@bb.com"
        a.save()        
        b = TestDetailModel(fk=a, name="test1")
        b.save()

        
        column = callbacks.get_model_change_href('fk', 'bbbbbb')
        value  = column(self.admin_site._registry[TestDetailModel], b)
        self.assertEqual(value , '<a class="modellink" href="/xadmin/xadmin/testmastermodel/1/">bbbbbb</a>')


        column = callbacks.get_model_change_href('fk')
        value  = column(self.admin_site._registry[TestDetailModel], b)
        self.assertEqual(value , '<a class="modellink" href="/xadmin/xadmin/testmastermodel/1/">TestMasterModel object</a>')


        column = callbacks.get_model_change_href('fk')
        column.label = "LABEL"
        value  = column(self.admin_site._registry[TestDetailModel], b)
        self.assertEqual(value , '<a class="modellink" href="/xadmin/xadmin/testmastermodel/1/">LABEL</a>')

    def test_get_model_change_href_for_invalid_field(self):        
        self._register(TestMasterModel, XModelAdmin)
        self._register(TestDetailModel, XModelAdmin)
        
        a = TestMasterModel()
        a.email_field =  "aa@bb.com"
        a.save()        
        b = TestDetailModel(fk=a, name="test1")
        b.save()

        column = callbacks.get_model_change_href('invalid_field')
        value  = column(self.admin_site._registry[TestDetailModel], b)
        self.assertEqual(value , '')

    def test_get_model_change_href_for_null_field(self):        
        self._register(TestMasterModel, XModelAdmin)
        self._register(TestDetailModel, XModelAdmin)
        
        a = TestMasterModel()
        a.email_field =  "aa@bb.com"
        a.save()        
        b = TestDetailModel(fk=None, name="test1")
        b.save()

        column = callbacks.get_model_change_href('fk')
        value  = column(self.admin_site._registry[TestDetailModel], b)
        self.assertEqual(value , '')
        